#include "ustc_comdef.h"
#include "ustc_custom.h"
#include "ustc_hif.h"
#include "ustc_api.h"

USTC_U16 ustc_tp_control_reg=0;

void ustc_tp_position(USTC_U16 *x, USTC_U16 *y, USTC_U16 *z)
{
//	outph(HIF_TP_RW_CMD, 0x8002);
	outph(HIF_TP_RW_CMD, 0x8012);
//	outph(HIF_TP_RW_CMD, 0x8002);
	ustc_delay(10000);
	*x=inph(HIF_TP_RW_CMD);

	
//	outph(HIF_TP_RW_CMD, 0x8003);
	outph(HIF_TP_RW_CMD, 0x8013);
//	outph(HIF_TP_RW_CMD, 0x8003);
	ustc_delay(10000);
	*y=inph(HIF_TP_RW_CMD);


//	outph(HIF_TP_RW_CMD, 0x8008);
	outph(HIF_TP_RW_CMD, 0x8018);
//	outph(HIF_TP_RW_CMD, 0x8008);
	ustc_delay(10000);
	*z=inph(HIF_TP_RW_CMD);

	outph(HIF_TP_RW_CMD, 0x800C);
}

void ustc_tp_init()
{
	ustc_tp_control_reg=0;
	outph(HIF_TP_RW_CMD, ustc_tp_control_reg);
	outph(HIF_TP_RW_CMD, 0x800C);
}

void ustc_tp_cmd(GS_TP_CMD_E cmd)
{
	outph(HIF_TP_RW_CMD, 0x8010|cmd);
}

USTC_U16 ustc_tp_value()
{
	return inph(HIF_TP_RW_CMD);
}

void ustc_tp_cfg_clk(USTC_U16 clk_div)
{
	ustc_tp_control_reg&=~0x60;
	ustc_tp_control_reg|=((clk_div&3)<<5);
	outph(HIF_TP_RW_CMD, ustc_tp_control_reg);
}

void ustc_tp_sleep(USTC_BOOL chip_sleep)
{
	if (chip_sleep)
		ustc_tp_control_reg|=0x4000;
	else
		ustc_tp_control_reg&=~0x4000;
		
	outph(HIF_TP_RW_CMD, ustc_tp_control_reg);
}

void ustc_tp_switch(USTC_U16 val)
{
	ustc_tp_control_reg&=~0x3800;
	ustc_tp_control_reg|=((val&7)<<11);
	outph(HIF_TP_RW_CMD, ustc_tp_control_reg);
}

void ustc_tp_power(USTC_BOOL power_down)
{
	if (power_down)
		ustc_tp_control_reg|=0x400;
	else
		ustc_tp_control_reg&=~0x400;
		
	outph(HIF_TP_RW_CMD, ustc_tp_control_reg);
}

void ustc_tp_int_enable()
{
	ustc_tp_control_reg|= 0x200;
	outph(HIF_TP_RW_CMD, ustc_tp_control_reg);
}

void ustc_tp_int_disable()
{
	ustc_tp_control_reg&=~0x200;
	outph(HIF_TP_RW_CMD, ustc_tp_control_reg);
}

void ustc_tp_cfg_mode(USTC_BOOL turbo_mode)
{
	if (turbo_mode)
		ustc_tp_control_reg|= 0x100;
	else
		ustc_tp_control_reg&=~0x100;

	outph(HIF_TP_RW_CMD, ustc_tp_control_reg);
}

void ustc_tp_sample_mode(USTC_BOOL double_sample)
{
	if (double_sample)
		ustc_tp_control_reg|=0x80;
	else
		ustc_tp_control_reg&=~0x80;
		
	outph(HIF_TP_RW_CMD, ustc_tp_control_reg);
}

